package cn.glinks.liuhe.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.glinks.liuhe.R;
import cn.glinks.liuhe.model.Question;

public class SwipeDeckAdapter extends BaseAdapter {

    private List<Question> data;
    private Context context;
    private LayoutInflater mInflater;

    public SwipeDeckAdapter(List<Question> data, Context context) {
        this.data = data;
        this.context = context;
    }

    private LayoutInflater getLayoutInflater(Context context) {
        if (mInflater == null) {
            mInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        return mInflater;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = getLayoutInflater(parent.getContext()).inflate(R.layout.view_question_card, null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        // 设置题目总数
        vh.sumText.setText(" / " + String.format("%03d", data.size()));
        // 设置题目编号
        vh.indexText.setText(data.get(position).indexStr);
        // 设置题目内容
        vh.contentText.setText(data.get(position).content);
        // 设置View的点击回调接口
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                这里可以扩展为详情页
//                Intent i = new Intent(v.getContext(), BlankActivity.class);
//                v.getContext().startActivity(i);
            }
        });

        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_qst_index)
        TextView indexText;
        @BindView(R.id.tv_qst_sum)
        TextView sumText;
        @BindView(R.id.tv_content)
        TextView contentText;
        @BindView(R.id.iv_left_image)
        ImageView leftBtn;
        @BindView(R.id.iv_right_image)
        ImageView rightBtn;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
